package com.estar.assistsurvy.widget;

import java.util.List;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.SelectCityAdapter;
import com.estar.assistsurvy.adapter.SelectProvinceAdapter;
import com.estar.assistsurvy.entity.CityVO;
import com.estar.assistsurvy.entity.ProvinceVO;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SelectAreaPopwindow extends PopupWindow {
//	private ListView bankList;
	private MyListView provinceListview,cityListview;
	private RelativeLayout cancel;
	private TextView selectName;
	private View v;
	private SelectProvinceAdapter provinceAdapter;
	private SelectCityAdapter cityAdapter;
	private List<CityVO> cityList;
	private List<ProvinceVO> areaList;
	private Context context;
	private OnItemClickListener cityItemClick;
	public SelectAreaPopwindow(Context context,OnItemClickListener provinceItemClick,
			OnItemClickListener cityItemClick,List<ProvinceVO> areaList){
		super(context);
		this.areaList=areaList;
		this.context=context;
		LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v=inflater.inflate(R.layout.popwindow_selectarea, null);
		provinceListview=(MyListView) v.findViewById(R.id.provinceListview);
		cityListview=(MyListView) v.findViewById(R.id.cityListview);
		cancel= (RelativeLayout) v.findViewById(R.id.cancel);
		selectName= (TextView) v.findViewById(R.id.selectName);
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		provinceAdapter=new SelectProvinceAdapter(areaList, context);
		provinceListview.setAdapter(provinceAdapter);
		provinceListview.setOnItemClickListener(provinceItemClick);
		cityList=areaList.get(0).getCityList();
		cityAdapter=new SelectCityAdapter(cityList, context);
		cityListview.setAdapter(cityAdapter);
		cityListview.setOnItemClickListener(cityItemClick);
		selectName.setText("请选择开户行所在省市名称");
		this.setContentView(v);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(android.R.style.Animation);
		ColorDrawable dw=new ColorDrawable(0xb0000000);
		this.setBackgroundDrawable(dw);
		v.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int height=v.findViewById(R.id.pop_layout).getTop();
				int y=(int) event.getY();
				if(event.getAction()==MotionEvent.ACTION_UP){
					if(y<height){
						dismiss();
					}
				}
				return true;
			}
		});
	}
	
	public void setCityList(int position){
		cityList=areaList.get(position).getCityList();
//		cityAdapter.notifyDataSetChanged();
		cityAdapter=new SelectCityAdapter(cityList, context);
		cityListview.setAdapter(cityAdapter);
		cityListview.setOnItemClickListener(cityItemClick);
	}
}
